package cn.lg.soar.system.biz.modules.monitor.controller;

import cn.lg.soar.common.model.HttpResult;
import cn.lg.soar.core.api.IResourceApi;
import cn.lg.soar.core.enums.LoggerLevelEnum;
import cn.lg.soar.mvc.annotations.Log;
import cn.lg.soar.common.enums.LogTypeEnum;
import cn.lg.soar.system.api.constant.ModuleConst;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author luguoxiang
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Slf4j
@Tag(name = "日志管理")
@RestController
@RequestMapping(ModuleConst.MODULE_NAME + "/logger")
public class LoggerController {

    @Autowired
    private IResourceApi resourceApi;

    @Operation(summary = "获取日志级别")
    @GetMapping(value = "getLevel", name = "获取日志级别")
    public HttpResult<Integer> getLevel(
            @RequestParam(value = "service", required = false) String service,
            @RequestParam("name") String name
    ) {
        LoggerLevelEnum loggerLevel = resourceApi.getLoggerLevel(service, name);
        return HttpResult.data(loggerLevel.getValue());
    }

    @Log(value = "修改日志级别", type = LogTypeEnum.OTHER)
    @Operation(summary = "修改日志级别")
    @PostMapping(value = "setLevel", name = "修改日志级别")
    public HttpResult<Integer> setLevel(
            @RequestParam(value = "service", required = false) String service,
            @RequestParam("name") String name,
            @RequestParam("level") LoggerLevelEnum level
    ) {
        LoggerLevelEnum loggerLevel = resourceApi.setLoggerLevel(service, name, level);
        return HttpResult.data(loggerLevel.getValue());
    }

    @Log(value = "改变sql日志", type = LogTypeEnum.OTHER)
    @Operation(summary = "改变sql日志")
    @PostMapping(value = "changeSqlLogger", name = "改变sql日志")
    public HttpResult<Void> changeSqlLogger(
            @RequestParam(value = "service", required = false) String service,
            @RequestParam("enabled") boolean enabled
    ) {
        resourceApi.changeSqlLogger(service, enabled);
        return HttpResult.success();
    }

}
